<?php namespace App\Http\Controllers\Api\Admin;

use App\Comic;
use App\ComicImgs;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;

class ComicController extends Controller
{
    public function setComic(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title  = $request->get('title');
        $desc   = $request->get('desc');
        $author = $request->get('author');
        $imgId  = $request->get('img_id');
        $id     = $request->get('id');

        $thatComic = Comic::whereId($id)->first();
        if(!$thatComic)
            $thatComic = new Comic();

        if(!is_null($title))$thatComic->title   = $title;
        if(!is_null($desc))$thatComic->desc     = $desc;
        if(!is_null($author))$thatComic->author = $author;
        if(!is_null($imgId))$thatComic->img_id  = $imgId;
        $thatComic->save();

        return response()->json(["level"=>"success","message"=>"设置成功","data"=>$thatComic]);
    }

    public function setComicImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id    = $request->get('id');
        $imgId = $request->get('img_id');

        $thatComic = Comic::whereId($id)->first();
        if(!$thatComic){
            return response()->json(['level'=>'error','message' => '漫画章节不存在！']);
        }

        $newImgs = new ComicImgs();
        $newImgs->comic_id = $id;
        $newImgs->img_id   = $imgId;
        $newImgs->save();

        return response()->json(["level"=>"success","message"=>"设置成功","data"=>$newImgs]);
    }

    public function delContents(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newContents = Comic::whereId($id)->first();

        if(!$newContents)
            return response()->json(['level'=>'error','message' => '没有该文章！']);

        $newContents->delete();

        return response()->json(["level"=>"success","message"=>"删除完成","data"=>$newContents]);
    }

    public function delComicImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newContents = ComicImgs::whereId($id)->first();

        if(!$newContents)
            return response()->json(['level'=>'error','message' => '没有该文章！']);

        $newContents->delete();

        return response()->json(["level"=>"success","message"=>"删除完成","data"=>$newContents]);
    }
}